var CryptoJS = require("crypto-js")

function get_sign(){
      var timestamp = new Date().getTime(), appSec='97e3d765c8574f35';
	  var path = "path"+'/api/pricetrend/getChartMultiCity.htm'+"timestamp"+timestamp+"version1.0.0"+appSec;
      return [CryptoJS.MD5(path).toString().toUpperCase(),timestamp]
}

var tools = {
  mappingObj: {},
  decryptEncryptAll: function (res, prop) {
    var json = JSON.parse(JSON.stringify(res)),
      base64Key = json.responseSecretKey,
      base64Iv = json.responseKeyOffset

    return this.decryptContent(json[prop], base64Key, base64Iv)
  },
  decryptEncryptFields: function (res, prop, isJsonFormat) {
    var json = JSON.parse(JSON.stringify(res)),
      copyTxt = isJsonFormat ? json[prop] : JSON.parse(json[prop]),
      base64Key = json.responseSecretKey,
      base64Iv = json.responseKeyOffset,
      base64Mapping = json.mapping,
      encryptField = json.encryptFields,
      mapping = this.decryptContent(base64Mapping, base64Key, base64Iv),
      mappings = mapping.split(',')

    for (var i = 0; i < mappings.length; i++) {
      var relative = mappings[i].split('-')
      this.mappingObj[relative[1]] = String(relative[0])
    }
    return this.modifyJson(copyTxt, encryptField)
  },
  decryptContent: function (content, key, iv) {
    var keyStr = CryptoJS.enc.Base64.parse(key),
      ivStr = CryptoJS.enc.Base64.parse(iv),
      base64 = CryptoJS.enc.Base64.parse(content),
      srcs = CryptoJS.enc.Base64.stringify(base64),
      descrypted = CryptoJS.AES.decrypt(srcs, keyStr, {
        iv: ivStr,
        mode: CryptoJS.mode.CBC,
        padding: CryptoJS.pad.Pkcs7
      })
    return CryptoJS.enc.Utf8.stringify(descrypted)
  },
  replaceStr: function (str, index, char) {
    var strAry = str.split('')
    strAry[index] = char
    return strAry.join('')
  },

  modifyJson: function (obj, encryptField) {
    for (var key in obj) {

      if (typeof obj[key] === 'string') {
        for (var i = 0; i < obj[key].length; i++) {
          if (encryptField.indexOf(key) != -1) {
            var tag = obj[key].charAt(i)
            if (this.mappingObj[tag]) {
              obj[key] = this.replaceStr(obj[key], i, this.mappingObj[tag])
            }
          }
        }
      } else if (Array.isArray(obj[key])) {
        for (var i = 0; i < obj[key].length; i++) {
          if (typeof obj[key] === 'object' && obj[key] !== null) {
            this.modifyJson(obj[key], encryptField)
          }
          if (encryptField.indexOf(key) != -1) {
            for (var j = 0; j < obj[key][i].length; j++) {
              var tag = obj[key][i].charAt(j)
              if (this.mappingObj[tag]) {
                obj[key][i] = this.replaceStr(
                  obj[key][i],
                  j,
                  this.mappingObj[tag]
                )
              }
            }
          }
        }
      } else if (typeof obj[key] === 'object' && obj[key] !== null) {
        this.modifyJson(obj[key], encryptField)
      }
    }
    return obj
  }
}


function decryptEncryptFields(res, prop, is_json){
    return tools.decryptEncryptFields(res, prop, is_json)
}


